package com.liuyfly.leetcode.questions;

import lombok.extern.slf4j.Slf4j;

import java.util.Stack;

/**
 * @author liuyangfang at 2022-10-09 19:36
 */
@Slf4j
public class Question0856 {
    /**
     * @param s 
     * @return
     * https://leetcode.cn/problems/score-of-parentheses/
     */
    public static int scoreOfParentheses(String s) {
        int score = 0;
        if (s.length() <= 1) {
            return score;
        }
        Stack<Character> stack = new Stack<>();
        Stack<Character> stack2 = new Stack<>();
        char[] array = s.toCharArray();
        int tempScore = 0;
        boolean pushFlag = false;
        for (char c : array) {
            if (c == '(') {
                stack.push(c);
                stack2.push(c);
                pushFlag = true;
            } else if (c == ')') {
                char temp = stack.pop();
                char temp2 = stack2.pop();
                if (temp == '(') {
                    if (pushFlag) {
                        tempScore++;
                    } else {
                        tempScore = tempScore * 2;
                    }
                }
                pushFlag = false;
            }
        }
        score = tempScore;
        return score;
    }

    public static void main(String[] args) {
        String str = "(()(()))";
        int result = scoreOfParentheses(str);
        log.info("result:{}", result);
    }
}
